from django.db import transaction
from users.models import UserProfile
from .models import PointTransaction


@transaction.atomic
def award_points(user, points, action_type, related_id=None):
    profile, _ = UserProfile.objects.get_or_create(user=user)
    profile.points = (profile.points or 0) + int(points)
    profile.save()
    PointTransaction.objects.create(user=user, points=int(points), action_type=action_type, related_id=related_id)